package in.cent.client.communication;

import in.cent.common.constants.CommunicationConstants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class CommunicationHandler
{
	private PrintWriter out = null;
	private BufferedReader in = null;
	private Socket sock = null;
	
	public CommunicationHandler(String server)
	{
		try
		{
			sock = new Socket(server, CommunicationConstants.PORT);
			out = new PrintWriter(sock.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
		} catch (UnknownHostException e)
		{
			e.printStackTrace();
			return;
		} catch (IOException e)
		{
			e.printStackTrace();
			return;
		}
		
		out.println(1);
		out.println("Hello There");

		new Thread()
		{
			public void run()
			{
				listen();
			}
		}.start();
	}
	
	private void listen()
	{
		try
		{
			out.close();
			in.close();
			sock.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
